package cn.itcast.itcaststore.serive.impl;

import cn.itcast.itcaststore.dao.OrderDao;
import cn.itcast.itcaststore.dao.ProductDao;
import cn.itcast.itcaststore.dao.impl.OrderDaoImpl;
import cn.itcast.itcaststore.dao.impl.ProductDaoImpl;
import cn.itcast.itcaststore.domain.*;
import cn.itcast.itcaststore.serive.OrderService;
import cn.itcast.itcaststore.utils.DataSourceUtils;

import java.sql.SQLException;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    private ProductDao productDao = new ProductDaoImpl();
    private String id;

    @Override
    public List<Orders> findAllOrder() throws SQLException {
        List<Orders> ordersList = null;
        try {
            ordersList = orderDao.findAllOrder();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return ordersList;
    }

    @Override
    public List<Orders> findOrderByManyCondition(String orderId, String receiverName) {
        List<Orders> orders = null;
        try {
          orders = orderDao.findOrderByManyCondition(orderId,receiverName);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return orders;
    }

    @Override
    public void addOrder(Orders order) {
        try {
            // 1.开启事务
            DataSourceUtils.startTransaction();
            // 2.完成操作
            // 2.1向orders表中添加数据
            orderDao.addOrder(order);
            // 2.2向orderItem表中添加数据
            orderDao.addOrderItem(order);
            // 2.3修改商品表中数据.
            productDao.changeProductNum(order);
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DataSourceUtils.rollback(); // 事务回滚
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            try {
                // 关闭，释放以及提交事务
                DataSourceUtils.releaseAndCloseConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Orders> findOrderByUser(User user) throws SQLException {
        List<Orders> orders = null;
        try {
            // 查找出订单信息
            orders = orderDao.findOrderByUser(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orders;
    }

    @Override
    public void updateOrderState(String id) throws SQLException {
        try {
            orderDao.updateOrderState(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateState(String r6_order) {
        try {
            orderDao.updateOrderState(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public Orders findOrderById(String orderId) {
        Orders order = null;
        try {
           order = orderDao.findOrderById(orderId);
          List<OrderItem> items = orderDao.findOrderItemByOrder(order);
           order.setOrderItems(items);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return order;
    }

    @Override
    public void delOrderById(String orderId) {
        //管理员删除订单
        try {
            DataSourceUtils.startTransaction();;//开启事务
            orderDao.delOrderItems(orderId);//删除订单项
            orderDao.delOrderById(orderId);//删除订单
        }catch (SQLException e){
            e.printStackTrace();
            try {
                DataSourceUtils.rollback();
            }catch (SQLException e1){
                e1.printStackTrace();
            }
        }finally {
            try {
                DataSourceUtils.releaseAndCloseConnection();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void delOrderByIdWithClient(String orderId) {
        try {
            DataSourceUtils.startTransaction();//开启事务
            //从订单项中查找商品购买数量
            Orders order=new Orders();
            order.setId(id);
            List<OrderItem> items=orderDao.findOrderItemByOrder(order);
            //修改商品数量
            productDao.updateProductNum(items);
            orderDao.delOrderItems(id); //删除订单项
            orderDao.delOrderById(id); //删除订单
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                DataSourceUtils.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }finally{
            try {
                DataSourceUtils.releaseAndCloseConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
